package com.baidu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.baidu.constant.MessageConstant;
import com.baidu.entity.PageResult;
import com.baidu.entity.QueryPageBean;
import com.baidu.entity.Result;
import com.baidu.pojo.CheckGroup;
import com.baidu.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController()
@RequestMapping("/checkgroup")
public class CheckGroupController {

	@Reference

	private CheckGroupService checkGroupService;

	@PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
	@RequestMapping("/add")
										//这里的传递过来的检查项id不是json格式的，可以不用加@RequestBody注解
	public Result addCheckGroup( Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){

		try {
			checkGroupService.addCheckGroup(checkitemIds,checkGroup);
			return  new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
		} catch (Exception e) {
			return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
		}


	}



	@RequestMapping("/findPage")
	public PageResult findPage( @RequestBody QueryPageBean queryPageBean){
		PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
		//System.out.println(pageResult.getRows()+"//"+pageResult.getTotal());
		return pageResult;

	}

	//回显检查组基本信息
	@RequestMapping("/findById")
	public Result findById( Integer id){

		try {
			CheckGroup checkGroup=	checkGroupService.findbyId(id);
			return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
		} catch (Exception e) {
			return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
		}


	}

	//回显当前检查组的检查项
	@RequestMapping("/findCheckItemIdsByCheckGroupId")
	public Result findCheckItemIdsByCheckGroupId( Integer id){


			List<Integer> CheckItemIds=	checkGroupService.findCheckItemIdsByGId(id);
			return new Result(true,"",CheckItemIds);



}
	@PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
	@PostMapping("/edit")
	public Result editCheckItem(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){


		try {
			checkGroupService.editCheckGroup(checkGroup,checkitemIds);
			return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
		} catch (Exception e) {
			return 	new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
		}


	}

	//查询所有检查组
	@RequestMapping("/findAll")
	public Result findAll(){
		try {
		List<CheckGroup> list=checkGroupService.findAll();
			return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
		} catch (Exception e) {
			return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
		}


	}


//删除检查组
	@DeleteMapping("/delete/{id}")
	public Result deleteByid(@PathVariable("id") Integer id){

		try {
			checkGroupService.deleteByid(id);
			return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
		} catch (Exception e) {
			return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
		}


	}






}
